﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("YUIAspNet");

YUIAspNet.TreeView = function(element) {
    YUIAspNet.TreeView.initializeBase(this, [element]);
}

YUIAspNet.TreeView.prototype = {
    initialize: function() {
        YUIAspNet.TreeView.callBaseMethod(this, 'initialize');
    },

    initializeYUIControl: function() {
        this._yuiControl = new YAHOO.widget.TreeView(this.get_TreeContainer());
        if (this.get_LazyLoad()) {

            var sender = this;
            var loadNodeFn = function(node, fnLoadComplete) {
                sender.LoadNodes(node);
                fnLoadComplete();
            };

            this._yuiControl.setDynamicLoad(loadNodeFn); // 0 or 1

        }

        if(this.get_Nodes())
            this._appendChilds(this._yuiControl.getRoot(), this.get_Nodes());

        if (this.get_events()) {
            var yuiTree = this;
            var treeContainer = this.get_TreeContainer();

            var clickHandler = this.get_events().getHandler("OnNodeClick");
            if (clickHandler) {
                if (!this.get_AutoPostback())
                    this._yuiControl.subscribe("clickEvent",
                        function(yuiArgs) {
                            var args = { node: yuiArgs.node, cancel: false };
                            clickHandler(yuiTree, args);
                            if (args.cancel) return false;
                        });
                else if (this.get_AutoPostback())
                    this._yuiControl.subscribe("clickEvent",
                        function(yuiArgs) {
                            var args = { node: yuiArgs.node, cancel: false };
                            clickHandler(yuiTree, args);
                            if (!args.cancel) yuiTree.DoPostback(yuiArgs.node.data.value);
                            else return false;
                        });
            }
            else if (this.get_AutoPostback()) {
                this._yuiControl.subscribe("clickEvent",
                        function(yuiArgs) {
                            yuiTree.DoPostback(yuiArgs.node.data.value);
                        });
            }

            var expandHandler = this.get_events().getHandler("OnNodeExpand");
            if (expandHandler)
                this._yuiControl.subscribe("expand",
                    function(yuiArgs) {
                        var args = { node: yuiArgs.node, cancel: false };
                        expandHandler(yuiTree, args);
                        if (args.cancel) return false;
                    });

            var collapseHandler = this.get_events().getHandler("OnNodeCollapse");
            if (collapseHandler)
                this._yuiControl.subscribe("collapse", function(node) { return collapseHandler(yuiTree, node); });
        }

        this._yuiControl.render();

        if (this.get_ExpandTo()) {
            var node = this._yuiControl.getNodeByProperty("value", this.get_ExpandTo());

            if (!node)
                var node = this._yuiControl.getNodeByProperty("label", this.get_ExpandTo());

            var tmpNode = node.parent;
            while (tmpNode) {
                tmpNode.expand();
                tmpNode = tmpNode.parent;
            }
        }
    },

    LoadNodes: function(node) {

        var ctl = this;

        var populateHandler = this.get_events().getHandler("OnNodePopulate");

        if (populateHandler) {
            populateHandler(ctl, node);
        }
        else alert("Tree was set to lazy, but no callback was defined to get the nodes!\n Please set the 'OnClientNodePopulate' property!");
    },

    dispose: function() {

        //Add custom dispose actions here
        this.get_YUIControl().destroy();
        YUIAspNet.TreeView.callBaseMethod(this, 'dispose');
    },

    get_TreeContainer: function() {
        return this._treeContainer;
    },
    set_TreeContainer: function(treeContainer) {
        this._treeContainer = treeContainer;
    },

    get_AutoPostback: function() {
        return this._autoPostback;
    },
    set_AutoPostback: function(autoPostback) {
        this._autoPostback = autoPostback;
    },

    get_ExpandTo: function() {
        return this._expandTo;
    },
    set_ExpandTo: function(expandTo) {
        this._expandTo = expandTo;
    },

    get_Nodes: function() {
        return this._nodes;
    },
    set_Nodes: function(nodes) {
        this._nodes = nodes;
    },

    get_LazyLoad: function() {
        return this._lazyLoad;
    },
    set_LazyLoad: function(lazyLoad) {
        this._lazyLoad = lazyLoad;
    },

    add_OnNodeClick: function(handler) {
        this.get_events().addHandler("OnNodeClick", handler);
    },
    remove_OnNodeClick: function(handler) {
        this.get_events().removeHandler("OnNodeClick", handler);
    },

    add_OnNodeExpand: function(handler) {
        this.get_events().addHandler("OnNodeExpand", handler);
    },
    remove_OnNodeExpand: function(handler) {
        this.get_events().removeHandler("OnNodeExpand", handler);
    },

    add_OnNodeCollapse: function(handler) {
        this.get_events().addHandler("OnNodeCollapse", handler);
    },
    remove_OnNodeCollapse: function(handler) {
        this.get_events().removeHandler("OnNodeCollapse", handler);
    },

    add_OnNodePopulate: function(handler) {
        this.get_events().addHandler("OnNodePopulate", handler);
    },
    remove_OnNodePopulate: function(handler) {
        this.get_events().removeHandler("OnNodePopulate", handler);
    },

    _appendChilds: function(nodeToAppend, nodeList) {
        for (var idx = 0; idx < nodeList.length; idx++) {
            var newNode = this._createAndAppend(nodeToAppend, nodeList[idx]);
            newNode.isLeaf = nodeList[idx].IsLeaf;

            if (nodeList[idx].Nodes && nodeList[idx].Nodes.length > 0)
                this._appendChilds(newNode, nodeList[idx].Nodes);
        }
    },

    _createAndAppend: function(parentNode, nodeData) {
        if (nodeData.HTML) {
            var options = { html: nodeData.HTML };
            return new YAHOO.widget.HTMLNode(options, parentNode, nodeData.Expanded);
        }
        else {
            var options = { label: nodeData.Text, data: { value: nodeData.Value ? nodeData.Value : nodeData.Text }, title: nodeData.Tooltip, href: nodeData.NavigateUrl, taget: nodeData.Target };
            return new YAHOO.widget.TextNode(options, parentNode, nodeData.Expanded);
        }
        //return null;
    }
}
YUIAspNet.TreeView.registerClass('YUIAspNet.TreeView', YUIAspNet.BaseControl);
